/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Entity.Bill;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Trung Duc
 */
public class BillTableModel extends AbstractTableModel {
    private List<Bill> listBill;
    ResourceBundle messages;
    public BillTableModel(List<Bill> listBill,ResourceBundle messages) {
        this.listBill = listBill;
        this.messages = messages;
    }
    
    public void changeStatus(int billID,int status){
        for(int i=0; i<listBill.size();i++){
            if(listBill.get(i).getId()==billID){
                listBill.get(i).setStatus(status);
            }
        }
    }
    public boolean Add(Bill bill){
        return listBill.add(bill);
    }
    public void remove(int billID){
        for(int i=0;i<listBill.size();i++){
            if(listBill.get(i).getId() == billID)
                listBill.remove(i);
        }
    }
    @Override
    public int getRowCount() {
        return listBill.size();
    }

    @Override
    public int getColumnCount() {
        return 6;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Bill bill = listBill.get(rowIndex);
        Timestamp timestamp = new Timestamp(bill.getDate().getTime());
        String date =timestamp.toString();
        String status ="";
        switch(bill.getStatus()){
            case 0: status = messages.getString("billNotApprove");break;
            case 1: status = messages.getString("billApproved");break;
            case 2: status = messages.getString("billCanceled");break;
        }
        switch(columnIndex){
            case 0: return bill.getId();
            case 1: return bill.getAccount();
            case 2: return bill.getFullName();
            case 3: return date;
            case 4: return bill.getTotal();
            case 5: return status;
            default: return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch(column){
            case 0: return messages.getString("billID");
            case 1: return messages.getString("account");
            case 2: return messages.getString("fullName");
            case 3: return messages.getString("date");
            case 4: return messages.getString("total");
            case 5: return messages.getString("status");
            default: return null;
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch(columnIndex){
            case 0: return Integer.class;
            case 1: return String.class;
            case 2: return String.class;
            case 3: return String.class;
            case 4: return Float.class;
            case 5: return String.class;
            default: return null;
    }
    }

    @Override
    public void fireTableDataChanged() {
        super.fireTableDataChanged();
    }
    
    public float getAmountOfMoneyTransacted(){
        float money = 0;
        for (Iterator<Bill> it = listBill.iterator(); it.hasNext();) {
            Bill bill = it.next();
            if(bill.getStatus() == 1)
                money+= bill.getTotal();
        }
        return money;
    }
    
    public int getAmountBillCancelled(){
        int count = 0;
        for (Iterator<Bill> it = listBill.iterator(); it.hasNext();) {
            Bill bill = it.next();
            if(bill.getStatus() == 1)
                count++;
        }
        return count;
    }
    
}
